import router from './router'
import { getToken } from './utils/auth'

const whiteList = ['/login', '/404']

router.beforeEach((to, from, next) => {
  console.log(123)
  // to:跳转的路由对象
  // from: 从那里来
  // next: 放行next() , 重定向next(路径)
  const token = getToken()
  if (token) {
    // 用户已登陆
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
    // 用户未登录
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
